﻿
/**
   ActionScript Class File -- Created with SAPIEN Technologies PrimalScript 3.1
   
   @class  
   @package
   @author 
   @codehint 
   @example 
   @tooltip 

   LoadMovieController 
   1) use for arrange loading sequence 
   2) prevent load too much file in 1 time which cause timeout
*/


class com.mixmedia.net.HTTPRequestQueue {
	private static var insArray:Array;
	
	private var requestArray:Array;
	private var firstRequest : Function;
	
	public static function instance(queueId:Number):HTTPRequestQueue{
		if(queueId==null)queueId=0;
		if(insArray==null)insArray = new Array();
		if(insArray[queueId]==null)insArray[queueId]= new HTTPRequestQueue();
		return insArray[queueId];
	}

	private function HTTPRequestQueue(){
		//creates the array that will contain our loading queue
		requestArray = new Array();
	}

	public function next() {
		// core function to loadMovie
		if (requestArray.length > 0){
			//if there is anything in the array, delete the first element,
			var req = requestArray.shift();
			if(firstRequest==req)req = requestArray.shift();
			req();
		}
	};

	public function addRequest(request:Function):Number{
	 	//adds another movie to the end of the array.
	 	if (requestArray.length==0){
	 		firstRequest = request;
	 		request();
	 	}
	 	return requestArray.push(request);
	}; 

	public function clear():Void{
	 	//clears the array, so no more loading will be started.
	 	requestArray = new Array();
	};
	
	public function clearAll():Void{
		for(var i:Number = 0;i<insArray.length;i++){
			insArray[i].clear();
		}
	}
	
	public function addNextRequest(request:Function){
	 	requestArray.splice(0,0,request);
	};
	
	public function remove(index:Number):Void{
		requestArray.splice(index,1);
	};
	
	public function getIndex(request:Function):Number{
		var i;
		for (i=0;i<requestArray.length;i++){
			if (requestArray[i] === request)return i;
		}
		return -1;
	};
}